package com.sijibao.amap;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.google.gson.Gson;
import com.yicai.sijibao.utl.ClientInfo;

public class LocationService extends Service implements AMapLocationListener {
	
	private LocationManagerProxy mAMapLocManager = null;
	
	public static String staticLocationJson;
	
	public static String getStaticLocationJson(){
		return staticLocationJson;
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		startLocation();
		return 1;
	}
	
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		stopLocation();
		super.onDestroy();
	}
	
	
	
	private void startLocation(){
		mAMapLocManager = LocationManagerProxy.getInstance(this);
		mAMapLocManager.setGpsEnable(true);
		/*
		 * mAMapLocManager.setGpsEnable(false);//
		 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true Location
		 * API定位采用GPS和网络混合定位方式
		 * ，第一个参数是定位provider，第二个参数时间最短是5000毫秒，第三个参数距离间隔单位是米，第四个参数是定位监听者
		 */
//		mAMapLocManager.requestLocationUpdates(
//				LocationProviderProxy.AMapNetwork, 5000, 100, this);
		mAMapLocManager.requestLocationData(LocationProviderProxy.AMapNetwork, -1, 100, this);
	
		
		
	}


	
	/**
	 * 销毁定位
	 */
	private void stopLocation() {
		if (mAMapLocManager != null) {
			mAMapLocManager.removeUpdates(this);
			mAMapLocManager.destory();
		}
		mAMapLocManager = null;
	}
	
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onLocationChanged(AMapLocation location) {
		
		if(location!=null){
			Log.i("eror", "总部一次");
			Gson gson = new Gson();
			MyAmapLocation myLocation = MyAmapLocation.amapLocationToMy(location);
			staticLocationJson = gson.toJson(myLocation);
			
			ClientInfo info=ClientInfo.build(getBaseContext());
			info.latitude=myLocation.latitude;
			info.longitude=myLocation.longitude;
			
			Intent intent = new Intent("com.sijibao.location.MyLocation");
//			intent.putExtra("latitude", location.getLatitude());
//			intent.putExtra("longitude", location.getLongitude());
//			intent.putExtra("province", location.getProvince());
			
//			MyLocation mylocation = LocationService20.toMyLocation(location);
			intent.putExtra("locationType", "amap");
			
			intent.putExtra("location", staticLocationJson);
			
			LocalBroadcastManager laBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
			laBroadcastManager.sendBroadcast(intent);
			stopLocation();
		}
		
	}
	
	
	
	

}
